package com.education.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @author 余一
 * @Description Bean实例工具类
 * @date 2021/6/13 11:36
 */
@Component
@Lazy(value = false)
public class SpringBeanManager implements ApplicationContextAware {

    private static ApplicationContext applicationContext=null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringBeanManager.applicationContext = applicationContext;
    }

    public static <T> T getBean(String name){
        return (T) applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> name){
        return (T) applicationContext.getBean(name);
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
